<?php
/**
 * This file is part of PHPWord - A pure PHP library for reading and writing
 * word processing documents.
 *
 * PHPWord is free software distributed under the terms of the GNU Lesser
 * General Public License version 3 as published by the Free Software Foundation.
 *
 * For the full copyright and license information, please read the LICENSE
 * file that was distributed with this source code. For the full list of
 * contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
 *
 * @see         https://github.com/PHPOffice/PHPWord
 * @copyright   2010-2018 PHPWord contributors
 * @license     http://www.gnu.org/licenses/lgpl.txt LGPL version 3
 */

namespace PhpOffice\PhpWord\Writer\HTML\Element;

use PhpOffice\PhpWord\Style\Font;
use PhpOffice\PhpWord\Style\Paragraph;
use PhpOffice\PhpWord\Writer\HTML\Style\Font as FontStyleWriter;
use PhpOffice\PhpWord\Writer\HTML\Style\Paragraph as ParagraphStyleWriter;

/**
 * TextRun element HTML writer
 *
 * @since 0.10.0
 */
class TextRun extends Text
{
    /**
     * Write text run
     *
     * @return string
     */
    public function write()
    {
        $content = '';
        $content .= $this->writeOpening();//这里构造p开始标签  <p style="text-align: left; text-indent: 10em;">
        $writer = new Container($this->parentWriter, $this->element);
        /*代码修改处*/
        $containerContent = $writer->write();
        $content .= $containerContent== "" ? "<span>&nbsp;</span>" : $containerContent;
        $content .= $this->writeClosing();
        /*代码修改处*/
        return $content;
    }

    /*代码修改处*/
    /**
     * Get font style.
     */
    private function getFontStyle()
    {
        /** @var \PhpOffice\PhpWord\Element\Text $element Type hint */
        $element = $this->element;
        $style = '';
        $fontStyle = $element->getFontStyle();
        $fStyleIsObject = ($fontStyle instanceof Font);
        if ($fStyleIsObject) {
            $styleWriter = new FontStyleWriter($fontStyle);
            $style = $styleWriter->write();
        }
        elseif (is_string($fontStyle)) {
            $style = $fontStyle;
        }
        return $style;
    }

    /**
     * Write paragraph style
     *此处原本应当继承父类Text的getParagraphStyle函数，但为了为便于 p 元素添加字体样式，单独构造该函数
     * @return string
     */
    private function getParagraphStyle()
    {
        /** @var \PhpOffice\PhpWord\Element\Text $element Type hint */
        $element = $this->element;
        $style = '';
        if (!method_exists($element, 'getParagraphStyle')) {
            return $style;
        }

        $paragraphStyle = $element->getParagraphStyle();
        $pStyleIsObject = ($paragraphStyle instanceof Paragraph);
        if ($pStyleIsObject) {
            $styleWriter = new ParagraphStyleWriter($paragraphStyle);
            $style = $styleWriter->write();
        }
        elseif (is_string($paragraphStyle)) {
            $style = $paragraphStyle;
        }
        if ($style) {
            $attribute = $pStyleIsObject ? 'style' : 'class';
//            $style = " {$attribute}=\"{$style}\"";
            $style = " {$attribute}=\"{$style}";//去掉最后的"引号，便于添加字体样式
        }

        return $style;
    }

    /**
     * 此处原本应当继承父类Text的writeOpening函数，但为了为 p 元素添加字体样式，单独构造该函数
     *
     * @return string
     */
    protected function writeOpening()
    {
        $content = '';
        $style = '';
        if (method_exists($this->element, 'getParagraphStyle')) {
            $style = $this->getParagraphStyle();
        }
        $style = str_replace(' margin-top: 0; margin-bottom: 0;', '', $style);
        $fontStyle = $this->getFontStyle();
        $content .= "<p{$style}{$fontStyle}\">";
        return $content;
    }
    /*代码修改处*/
}
